//
//  CYCouponsContentViewController.m
//  cyjf
//
//  Created by tauCross on 2017/9/28.
//  Copyright © 2017年 Auto Financing. All rights reserved.
//

#import "CYCouponsContentViewController.h"

#import "CYCouponCell.h"

#import "CYMainController.h"



@interface CYCouponsContentViewController ()

@property(nonatomic, strong)UIView *noneView;
@property(nonatomic, strong)UIImageView *noneImageView;
@property(nonatomic, strong)UILabel *noneHintLabel;

@property(nonatomic, assign)NSInteger page_no;
@property(nonatomic, assign)BOOL loading;
@property(nonatomic, assign)BOOL hasMore;

@end


@implementation CYCouponsContentViewController

- (void)loadUI
{
    self.contentView.backgroundColor = HEXCOLOR(@"f7f7f7");
    
    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 15)];
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 15)];
    
    [self loadNoneView];
    
    self.tableView.hidden = YES;
    self.noneView.hidden = YES;
}

- (void)loadNoneView
{
    NSString *hintString = nil;
    switch(self.status)
    {
        case CYDataAPICouponStatusAvailable:
            hintString = @"暂时还没有优惠券\n请关注车蚁金服更多活动";
            break;
        case CYDataAPICouponStatusUsed:
            hintString = @"您还未使用过优惠券";
            break;
        case CYDataAPICouponStatusDisable:
            hintString = @"没有失效的优惠券";
            break;
    }
    
    self.noneView = [[UIView alloc] init];
    
    self.noneImageView = [[UIImageView alloc] initWithImage:CYImage(@"empty_ico_coupon")];
    [self.noneView addSubview:self.noneImageView];
    
    self.noneHintLabel = [[UILabel alloc] init];
    self.noneHintLabel.numberOfLines = 0;
    self.noneHintLabel.textAlignment = NSTextAlignmentCenter;
    self.noneHintLabel.font = [UIFont systemFontOfSize:16];
    self.noneHintLabel.textColor = HEXCOLOR(@"999999");
    self.noneHintLabel.text = hintString;
    [self.noneHintLabel sizeToFit];
    [self.noneView addSubview:self.noneHintLabel];
    
    [self.contentView addSubview:self.noneView];
}

- (void)layoutUI
{
    self.noneView.frame = self.contentView.bounds;
    {
        self.noneImageView.centerX = self.noneView.width / 2;
        self.noneImageView.top = 50;
        
        self.noneHintLabel.centerX = self.noneView.width / 2;
        self.noneHintLabel.centerY = self.noneImageView.bottom + 26;
    }
}

- (void)loadData
{
    self.page_no = 1;
    self.hasMore = YES;
    self.loading = NO;
    self.tableModel = [[NIMutableTableViewModel alloc] initWithSectionedArray:@[@""] delegate:(id)[NICellFactory class]];
    self.tableView.dataSource = self.tableModel;
    [self loadDataFromServer];
}

- (void)loadDataFromServer
{
    if(self.loading)
    {
        return;
    }
    if(self.hasMore == NO)
    {
        return;
    }
    self.loading = YES;
    @weakify(self);
    [CYStandardDataAPI user_queryCouponWithStatus:self.status page_no:@(self.page_no) complete:^(CYResponse *response) {
        @strongify(self);
        if(response.code == 0)
        {
            if([response.content isKindOfClass:[NSArray class]])
            {
                self.hasMore = [response.content count] >= CY_API_PAGE_SIZE;
                if([response.content count] == 0 && self.page_no == 1)
                {
                    self.noneView.hidden = NO;
                }
                else
                {
                    self.tableView.hidden = NO;
                    self.page_no ++;
                    NSArray *models = [CYDataAPICouponModel mj_objectArrayWithKeyValuesArray:response.content];
                    NSMutableArray *cells = [NSMutableArray array];
                    for(CYDataAPICouponModel *model in models)
                    {
                        CYCouponCellObject *cell = [CYCouponCellObject objectWithCellClass:[CYCouponCell class]];
                        cell.model = model;
                        cell.useAction = ^{
                            [[CYMainController standardController] goFinance];
                        };
                        cell.expandAction = ^{
                            @strongify(self);
                            [self.tableView reloadData];
                        };
                        [cells addObject:cell];
                    }
                    [self.tableModel addObjectsFromArray:cells];
                    [self.tableView reloadData];
                }
            }
        }
        self.loading = NO;
    }];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if(scrollView == self.tableView)
    {
        if(scrollView.contentSize.height - scrollView.contentOffset.y < scrollView.height * 2)
        {
            [self loadDataFromServer];
        }
    }
}

@end
